Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports ClubStarterKit.Data

Namespace ClubStarterKit.Web
    Public Class UserAdministration

        Public Shared Function NeedsEmailVerification() As Boolean
            Return ClubStarterKit.Web.Settings.EmailVerification()
        End Function

        Public Shared Function NeedsAdminVerification() As Boolean
            Return ClubStarterKit.Web.Settings.AdminVerification()
        End Function

        Public Shared Sub SendEmailVer(ByVal User As MembershipUser)
            If NeedsEmailVerification() = True Then
                Dim MemInfo As New MemberInfo(MemberInfo.Columns.Memberid, User.ProviderUserKey())

                Dim Name As String = ""

                Dim from As New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name() & " Webmaster")
                If MemInfo.IsLoaded = True Then
                    Name = MemInfo.Firstname & " " & MemInfo.Lastname
                Else
                    Name = "Future Member"
                End If
                Dim toaddr As New MailAddress(User.Email(), Name)
                Dim msg As New MailMessage(from, toaddr)

                Dim URL As String = ClubStarterKit.Web.Settings.SiteURL() & "Member/EmailVerification.aspx?username=" & User.UserName()

                msg.Subject = "Email Verification"

                Dim body As String = "We thank you for signing up for " & ClubStarterKit.Web.Settings.Name() & " membership."
                body += " We need to verify that your email is correct. Please click the link below, or copy and paste it into your web browser. <br />"
                body += "<a href='" & URL & "'>" & URL & "</a>"
                msg.Body = body

                msg.IsBodyHtml = True

                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred

                smtp.Send(msg)
            End If
        End Sub

        Public Shared Sub SendAdminVer(ByVal User As MembershipUser)
            If NeedsAdminVerification() = True Then
                Dim MemInfo As New MemberInfo(MemberInfo.Columns.Memberid, User.ProviderUserKey())

                Dim from As New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name() & " Webmaster")

                Dim msg As New MailMessage(from, from)

                Dim URL As String = ClubStarterKit.Web.Settings.SiteURL() & "Member/AdminVerification.aspx?username=" & User.UserName()

                msg.Subject = "Admin Verification for " & User.UserName()

                Dim body As String = "User " & User.UserName() & " required your approval."
                body += " Here are their contact details: <br /><br />"
                body += "<strong>First Name</strong>: "
                body += MemInfo.Firstname
                body += "<br /><strong>Last Name</strong>: "
                body += MemInfo.Lastname
                body += "<br /><strong>Phone</strong>: "
                body += MemInfo.Phone
                body += "<br /><strong>Address</strong>: "
                body += MemInfo.Address

                body += "<br /><br /><a href='" & URL & "'>" & URL & "</a>"
                msg.Body = body

                msg.IsBodyHtml = True

                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred

                smtp.Send(msg)
            End If
        End Sub

        Public Shared Sub ApprovedEmail(ByVal user As MembershipUser)
            Dim MemInfo As New MemberInfo(MemberInfo.Columns.Memberid, user.ProviderUserKey())

            Dim Name As String = ""

            Dim from As New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name() & " Webmaster")
            If MemInfo.IsLoaded = True Then
                Name = MemInfo.Firstname & " " & MemInfo.Lastname
            Else
                Name = "Future Member"
            End If
            Dim toaddr As New MailAddress(user.Email(), Name)
            Dim msg As New MailMessage(from, toaddr)

            msg.Subject = "Approved!"

            Dim body As String = "You have finally been approved for the " & ClubStarterKit.Web.Settings.Name() & " website!"
            msg.Body = body

            msg.IsBodyHtml = False

            Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
            Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
            smtp.UseDefaultCredentials = False
            smtp.Credentials = cred

            smtp.Send(msg)
        End Sub
    End Class
End Namespace